package com.ruoyi.house_sale.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.house_sale.domain.HouseEvent;
import com.ruoyi.house_sale.domain.PaidScheme;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.house_sale.domain.BuyHouse;
import com.ruoyi.house_sale.service.IBuyHouseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 购买房产Controller
 *
 * @author ruoyi
 * @date 2022-04-08
 */
@RestController
@RequestMapping("/house_sale/BuyHouse")
public class BuyHouseController extends BaseController
{
    @Autowired
    private IBuyHouseService buyHouseService;

    /**
     * 查询付款方案列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:PaidScheme:list')")
    @GetMapping("/paidschemelist")
    public TableDataInfo list(PaidScheme paidScheme)
    {
        startPage();
        List<PaidScheme> list = buyHouseService.selectPaidSchemeList(paidScheme);
        return getDataTable(list);
    }
    /**
     * 查询购买房产列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:BuyHouse:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuyHouse buyHouse)
    {
        startPage();
        List<BuyHouse> list = buyHouseService.selectBuyHouseList(buyHouse);
        return getDataTable(list);
    }

    /**
     * 查询购买房产列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:BuyHouse:list')")
    @GetMapping("/list1")
    public TableDataInfo findAllhouseList(BuyHouse buyHouse)
    {
        startPage();
        List<BuyHouse> list = buyHouseService.findAllhouseList(buyHouse);
        return getDataTable(list);
    }

    /**
     * 导出购买房产列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:BuyHouse:export')")
    @Log(title = "购买房产", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyHouse buyHouse)
    {
        List<BuyHouse> list = buyHouseService.selectBuyHouseList(buyHouse);
        ExcelUtil<BuyHouse> util = new ExcelUtil<BuyHouse>(BuyHouse.class);
        util.exportExcel(response, list, "购买房产数据");
    }

    /**
     * 获取购买房产详细信息
     */
    @PreAuthorize("@ss.hasPermi('house_sale:BuyHouse:query')")
    @GetMapping(value = "/{buyid}")
    public AjaxResult getInfo(@PathVariable("buyid") Long buyid)
    {
        return AjaxResult.success(buyHouseService.selectBuyHouseByBuyid(buyid));
    }

    /**
     * 新增购买房产
     */
    @PreAuthorize("@ss.hasPermi('house_sale:BuyHouse:add')")
    @Log(title = "购买房产", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyHouse buyHouse)
    {
        buyHouse.setOprtime(new Date());
        buyHouse.setUserid(SecurityUtils.getUsername());
        buyHouseService.insertBuyHouse(buyHouse);
        return AjaxResult.success(buyHouse.getBuyid());
    }

    /**
     * 修改购买房产
     */
    @PreAuthorize("@ss.hasPermi('house_sale:BuyHouse:edit')")
    @Log(title = "购买房产", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyHouse buyHouse)
    {
        return toAjax(buyHouseService.updateBuyHouse(buyHouse));
    }

    /**
     * 删除购买房产
     */
    @PreAuthorize("@ss.hasPermi('house_sale:BuyHouse:remove')")
    @Log(title = "购买房产", businessType = BusinessType.DELETE)
    @DeleteMapping("/{buyids}")
    public AjaxResult remove(@PathVariable Long[] buyids)
    {
        return toAjax(buyHouseService.deleteBuyHouseByBuyids(buyids));
    }

    /**
     * 退房
     */
    @PreAuthorize("@ss.hasPermi('house_sale:BuyHouse:checkout')")
    @Log(title = "退房", businessType = BusinessType.OTHER)
    @PostMapping("/checkout")
    public AjaxResult checkout(@RequestBody HouseEvent houseEvent) {
        buyHouseService.checkout(houseEvent);
        return AjaxResult.success();
    }

    /**
     * 换房
     */
    @PreAuthorize("@ss.hasPermi('house_sale:BuyHouse:change')")
    @Log(title = "换房", businessType = BusinessType.OTHER)
    @PostMapping("/change")
    public AjaxResult change(@RequestBody BuyHouse buyHouse) {

        return AjaxResult.success(buyHouseService.change(buyHouse));
    }
}
